<%-- 
    Document   : CirclePage
    Created on : Dec 4, 2011, 3:34:49 PM
    Author     : Michael
--%>

<%@page import="java.sql.*"%>
<%--@page import="java.sql.ResultSet"--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<%
    String circID;
    if(request.getParameterNames().hasMoreElements()==false)
    {
        circID=session.getAttribute("CIRC").toString();
    }
    else
    circID = request.getParameter("CIRC");
    session.setAttribute("CIRC", circID);
    //System.out.println(circID);
    Connection conn = null;
            try
            {
                conn = new DBClasses.GetConnection().getConnection();
                String query = "SELECT * FROM Circle WHERE CircleID='" + circID + "'";
                
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                
                rs.next();
                String circName = rs.getString("CircleName");
                int circleOwner = rs.getInt("CircleOwner");
                int usrID = Integer.parseInt(session.getAttribute("UserID").toString());
%>  
<table width="800" border="1">
    <tr><td><img src="Logo.jpg" width="800" height="150" /></td></tr>
    
    <tr><td>
<input type="button" value="Home" onclick="window.location='gohome.jsp'"/>&nbsp;
  <input type="button" value="Log Out" onclick="window.location='logout.jsp'"/>
        </td></tr>
  <tr>
    <td align="center">
        <h2>Circle Info</h2>             
        <form  name="circleinfo" action="circleInfo.jsp" method="post">
            <input type="hidden" name="CIRC" value="<%=circID%>" />
            <label>Name: </label>
            <input type="text" name="circlename" value="<%=rs.getString("CircleName") %>"
                   <% if(circleOwner!=usrID) {%> 
                   readonly="readonly" <% } %>/><br />
            <label>Type: </label>
            <input type="text" name="circletype" value="<%=rs.getString("CircleType") %>"
                   <% if(circleOwner!=usrID) {%> 
                   readonly="readonly" <% } %>/><br />
        </form><br />
        <%
        if (circleOwner==usrID) {%>
        <form name="addusers" action="SearchPeople.jsp" method="post">
            <input type="hidden" name="CIRC" value="<%=circID%>" />
            <input type="submit" value="Search+Add users"/>
        </form>
        <%;}
            //query to get the owners name
            rs = stmt.executeQuery("SELECT Email FROM customer WHERE CustomerID=" + circleOwner);
            rs.next();
%>
            <label>Owner: </label>
            <input name="owner" readonly="readonly" value="<%=rs.getString("Email") %>"/><br /><br />
            
           
               <input type= "button" value = "View Members" onclick= "document.viewmembs.submit()"/>
            <%
                        //Find out if the user is in this circle
            rs = conn.createStatement().executeQuery("SELECT * FROM circlemembership WHERE CustomerID=" + usrID + " AND CircleID=" + circID);
            if(rs.next()) {

            
                if(circleOwner!=usrID){
            %>
            <input type="button" value ="Unjoin" onclick="document.unjoin.submit()"/>
            <%
            }//end if
            if(circleOwner==usrID){%>
            <input type="button" value ="Update" onclick="document.circleinfo.submit()"/>
            <input type="button" value ="Delete Circle" onclick="var r = confirm('Really Delete Circle?');
               if(r==true)
                document.deletecircle.submit()"/> <% } %>
            
            <form  name="deletecircle" action="deleteCircle.jsp" method="post">
             <input type="hidden" name="CIRC" value="<%=circID%>" />
            </form>
            <form name="unjoin" action="unjoin.jsp" method="post">
                <input type="hidden" name="CIRC" value="<%=circID%>"/>
            </form>
             <form name= "viewmembs" action= "ViewMembers.jsp" method= "post">
                <input type= "hidden" name= "CIRC" value= "<%=circID%>"/>
                <input type="hidden" name= "OWNR" value= "<%=circleOwner%>"/>
                <input type="hidden" name="NAME" value="<%=circName%>"/>
             </form>

    </td>
  </tr>
<%            
            //Get the page
            query =  "SELECT * FROM Pages WHERE AssociatedCircleID='" + circID + "'";
            rs = stmt.executeQuery(query);
            rs.next();
            String pageID = rs.getString("PageID");
            
%>
  <tr>
  <form name="makePost" action="createPost.jsp" method="post">
        <input type="hidden" name="CIRC" value="<%=circID%>" />
    <td><input name="postText" type="text" value=""/>
        <input type="button" value="post" onclick="document.makePost.submit()"/>
    </td>
  </form>
  </tr>
  <tr>
      <td>
          <h4>Posts: </h4>


  <%
            query = "SELECT * FROM Posts WHERE PageID='"+pageID+"' ORDER BY Date DESC";
            rs = stmt.executeQuery(query);
            int i=0;
            while(rs.next())//start while1 loop
            {   
                String postID = rs.getString("PostID");
                String fname = "f"+i;
                String dname = "d"+i;
                query ="SELECT Firstname,Lastname FROM Customer WHERE CustomerID = "
                        +rs.getString("Author");
                Statement aStmt = conn.createStatement();
                ResultSet aRS = aStmt.executeQuery(query);
                aRS.next();
                String postAuthor = aRS.getString("Firstname")+" "+aRS.getString("Lastname");
  %>
<table width="800"  >
  <tr bgcolor="#CCCCCC">
      
          <td width="175"><label name ="author"><%=postAuthor %></label></td>
          <td width="175"><label name = "date"><%=rs.getString("Date") %></label></td>
          <td><label name = "content"><%=rs.getString("Content") %></label></td>
          <td><form name="<%="Like Post"+i%>" action="LikePost.jsp" method="post">
              <input name="POST" type="hidden" value="<%=postID%>"/>
              <input name="CIRC" type="hidden" value="<%=circID%>"/>
              <%
              java.sql.Statement stmt4 = conn.createStatement();
              String query3 = "SELECT * "
                      + "FROM post_likes P "
                      + "WHERE P.PostID='" +postID + "' AND P.CustomerID=" +usrID;
              java.sql.ResultSet rs4 = stmt4.executeQuery(query3);
              int count = 0;
              while(rs4.next())
                      count++;
              
              String likeUnlike = "";
              if(count>0)
                  likeUnlike="Unlike";
              else
                  likeUnlike="Like";
              %>
              <input type="submit" value="<%=likeUnlike%>" />                <!--onclick="document.forms['LikePost'].submit();"-->  
             </form></td>
             <%
            //get author of the post
            int authID = rs.getInt("Author");

            //author or circle owner can delete a post
            if(authID==usrID||circleOwner==usrID)
            {
          %>
          <form name= "<%=dname%>" action= "deletePost.jsp" method= "post">
              <input type= "hidden" name= "PST" value= "<%=postID%>"/>
              <input type="hidden" name= "CIRC" value= "<%=circID%>"/>
              <td><input type= "button" value = "Delete" onclick= "document.<%=dname%>.submit()"/></td>
          </form>
          <%
            }//end if
            //author can edit a post
            if(authID==usrID)
            {
          %>
          <form name="<%="e"+i%>">
              <td><input type="button" value="Edit" /></td>
          </form>
          <%}//end if
          %>
          <table cellspacing="0" style="margin-left: 100px;" >
              <%
                //query for comments, put comment info in table
                query = "SELECT * FROM Comments WHERE PostID ='"+postID+"'";
                Statement stmt2 = conn.createStatement();
                ResultSet rs2 = stmt2.executeQuery(query);
                
                int r = 0;
                while(rs2.next()) // start while 2 loop
                {
                    String cname = "c"+r;
                    String commentID = rs2.getString("CommentID");
                    authID = rs2.getInt("Author");
                    query = "SELECT Firstname,Lastname FROM Customer WHERE CustomerID = "
                            +rs2.getString("Author");
                    Statement stmt3 = conn.createStatement();
                    ResultSet rs3 = stmt3.executeQuery(query);
                    rs3.next();
                    String author = rs3.getString("Firstname")+" "+rs3.getString("Lastname");
              %>
              <tr bgcolor="#66CCFF">
                  <td><label name ="author"><%=author %></label><strong>&nbsp;On&nbsp;</strong></td>
                  <td><label name = "date"><%=rs2.getString("Date") %></label><strong>&nbsp;Wrote:&nbsp;</strong></td>
                  <td><label name = "content"><%=rs2.getString("Content") %></label></td>
                  <td><form name="<%="LikeComment" + i + " " +r%>" action="LikeComment.jsp" method="post">
                    <input name="COMM" type="hidden" value="<%=commentID%>"/>
                    <input name="CIRC" type="hidden" value="<%=circID%>"/>
                    <%
                    java.sql.Statement stmt5 = conn.createStatement();
                    String query4 = "SELECT * "
                      + "FROM comment_likes C "
                      + "WHERE C.CommentID='" +commentID + "' AND C.CustomerID=" +usrID;
                      java.sql.ResultSet rs5 = stmt5.executeQuery(query4);
                    int comCount = 0;
                    while(rs5.next())
                        comCount++;
              
                    String likeUnlikeComment = "";
                    if(comCount>0)
                        likeUnlikeComment="Unlike";
                    else
                        likeUnlikeComment="Like";
              %>
                    <input type="submit" value="<%=likeUnlikeComment%>"  /></form></td> <!--onclick="document.forms['LikeComment'].submit();"-->
                  <%
                    //author and owner can delete comments
                    if(authID==usrID||circleOwner==usrID)
                    {
                  %>
                  <form name= "<%=cname%>" action= "deleteComment.jsp" method= "post"> 
                      <input type= "hidden" name = "CIRC" value= "<%=circID%>"/>
                      <input type= "hidden" name = "CMT" value= "<%=commentID%>"/>
                      <td><input type="button" value="Delete" onclick= "document.<%=cname%>.submit()"/></td>
                  </form>
                  <%
                    }//end if
                    if(authID==usrID)
                    {
                  %>
                        <td><input type="button" value="Edit" /></td>
                  <%}//endif%>
          </tr>
         

  <%        
            r++;
                }//end while 2 loop
                %>
          <tr bgcolor="#66CCFF">
              <form name= "<%=fname%>" action = "createComment.jsp" method= "post">
                  <input type= "hidden" name= "PST" value= "<%=postID%>" />
                  <td><input type= "text" name= "commentText"/></td>
                  <td><input type= "button" value= "New Comment" onclick= "document.<%=fname%>.submit()"/></td>
              </form>
          </tr>
      </table>
<%
            i++;
            }//end while1 loop     

%>
     </tr>  
          
          </table>
      </td>
  </tr>
</table>
            <%
                       }
            else
            { 
                session.setAttribute("Owner", circleOwner);
                session.setAttribute("CIRC", circID);%>
                <button onclick="var r = confirm('Send a request to join this circle?'); if(r==true) window.location='joinCircle.jsp'">Join</button>
           <% }
            }//end try block
                   catch(Exception e)
                   {
                       e.printStackTrace();
                   }
    
                   finally
                   {
                       try{
                       conn.close(); }
                       catch(Exception e) {
                           e.printStackTrace();
                       }
                   }
%>
</body>
</html>
